package com.sxp.servlet;

import com.sxp.bean.Book;
import com.sxp.dao.BookDao;
import com.sxp.dao.impl.BookDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author hsowan
 * @date 2019-06-13
 **/
@WebServlet("/book/detail")
public class BookDetailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int bookId = Integer.parseInt(req.getParameter("id"));
        BookDao bookDao = new BookDaoImpl();
        Book book = bookDao.getById(bookId);
        HttpSession session = req.getSession();
        if (book != null) {
            book.setClickCount(book.getClickCount() + 1);
            bookDao.update(book);
            session.setAttribute("book", book);
            req.getRequestDispatcher("/detail.jsp").forward(req, resp);
        } else {
            session.setAttribute("msg", "别乱来，兄dei！");
            resp.sendRedirect("/obs/message.jsp");
        }



    }
}
